class String
  def oinclude?(thisin,startfrom)
    self[startfrom..self.length].include?(thisin)
  end
end
class Sim
      ColorInfo = Struct.new(
	:bold,      # bool, Bold/Bright Color, or Dim
	:underline, # bool, text underline or regular
	:blink,     # bool, flashing/blinking text or solid
	:fgcol,     # Gdk::Color, Color of the text
	:bgcol,     # Gdk::Color, Background of the text
	:text,      # Range, Text to be modified with

	:link)			# Link Data, if any
  def initialize

    @Black,@Crimson,@Forest,@Brown,@Navy,@Violet,@Aqua,@Gray,@Gloom,@Red,@Green,@Yellow,@Blue,@Purple,@Cyan,@White="black","red","green","brown","navy",
                  "violet","aquamarine","grey","dim grey","red","green","yellow","blue","purple","cyan","white"
    @Colors = [@Black,@Crimson,@Forest,@Brown,@Navy,@Violet,@Aqua,@Gray,@Gloom,@Red,
									@Green,@Yellow,@Blue,@Purple,@Cyan,@White]
                  @CIndx = {}; @Colors.each_with_index {|color, i| @CIndx[color] = i}
  end
  def parse(text)

		newText = []

		tag = ColorInfo.new(false,false,false,"grey","black","")

		if text.include?(27.chr)

			iis = text.index(27.chr + '[')

			if iis > 0

				tag[:text] = text[0..iis-1]

				newText << tag.dup

			end

			i = iis

			while text.oinclude?(27.chr + '[',i)

				iis = text.index(27.chr + '[',i)

				iie = text.index('m',i)

				if iie < iis

					iie = text.index('m',iis)

				end

				code = text[iis+2..iie-1]

				i = iie+1

				iis = text.index(27.chr + '[',i)

				if iis.nil?

					tag[:text] = text[i..text.length]

				else

					tag[:text] = text[i..iis-1]

				end

				code = code.split(";")

				

				code.each do |zx|
z=zx.to_i
					case z

					when 0

						tag[:bold] = false

						tag[:underline] = false

						tag[:blink] = false

						tag[:fgcol] = "grey"

						tag[:bgcol] = "black"

					when 1

						tag[:bold] = true

						if code.length == 1

							tag[:fgcol] = "grey"

						tag[:bgcol] = "black"

						end

					when 4

						tag[:underline] = true

					when 5

						tag[:blink] = true

					when 30..37

						cind = z - 30

						if tag[:bold]

							cind = cind + 8

						end

						tag[:fgcol] = @Colors[cind]

					when 40..47

						cind = z - 40

						tag[:bgcol] = @Colors[cind]

					end

				end

				newText << tag.dup

			end

		else

			tag[:text] = text

			newText << tag

		end

		newText
    
  
	end
end

  #::find "red"
